package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsInboundAbnormalMissionMapper;
import me.zhengjie.pojo.entity.WcsInboundAbnormalMission;
import me.zhengjie.sqlservice.WcsInboundAbnormalMissionService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WcsInboundAbnormalMissionServiceImpl extends ServiceImpl<WcsInboundAbnormalMissionMapper,WcsInboundAbnormalMission> implements WcsInboundAbnormalMissionService {

    private final WcsInboundAbnormalMissionMapper wcsInboundAbnormalMissionMapper;

    @Override
    public boolean insertAbnormal(WcsInboundAbnormalMission wcsInboundAbnormalMission) {
        return wcsInboundAbnormalMissionMapper.insert(wcsInboundAbnormalMission) >0;
    }

    @Override
    public List<WcsInboundAbnormalMission> selectContainerCode(String threeCode) {
        LambdaQueryWrapper<WcsInboundAbnormalMission> wcsInboundAbnormalMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundAbnormalMissionLambdaQueryWrapper.eq(WcsInboundAbnormalMission::getContainerCode,threeCode)
                .orderByDesc(WcsInboundAbnormalMission::getUpdateTime);
        return wcsInboundAbnormalMissionMapper.selectList(wcsInboundAbnormalMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateMissionStatus(String taskCode, Integer missionStatus) {
        LambdaUpdateWrapper<WcsInboundAbnormalMission> wcsInboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsInboundMissionLambdaUpdateWrapper.eq(WcsInboundAbnormalMission::getAbnormalCode, taskCode)
                .set(WcsInboundAbnormalMission::getMissionStatus, missionStatus)
                .set(WcsInboundAbnormalMission::getUpdateTime, LocalDateTime.now());
        return wcsInboundAbnormalMissionMapper.update(null,wcsInboundMissionLambdaUpdateWrapper) > 0;
    }

    @Override
    public WcsInboundAbnormalMission selectInboundAbnormalCode(String abnormalCode) {
        LambdaQueryWrapper<WcsInboundAbnormalMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.eq(WcsInboundAbnormalMission::getAbnormalCode,abnormalCode);
        return wcsInboundAbnormalMissionMapper.selectOne(wcsDeliveryMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateAbnormalStatusAndAgvId(Integer id, Integer missionStatus, String agvId) {
        LambdaUpdateWrapper<WcsInboundAbnormalMission> wcsDeliveryMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsDeliveryMissionLambdaUpdateWrapper.set(WcsInboundAbnormalMission::getMissionStatus,missionStatus)
                .set(WcsInboundAbnormalMission::getAgvId,agvId)
                .set(WcsInboundAbnormalMission::getUpdateTime, LocalDateTime.now())
                .eq(WcsInboundAbnormalMission::getId,id);
        return wcsInboundAbnormalMissionMapper.update(null,wcsDeliveryMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public List<WcsInboundAbnormalMission> selectMissionStatus(Integer status) {
        LambdaQueryWrapper<WcsInboundAbnormalMission> wcsInboundAbnormalMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundAbnormalMissionLambdaQueryWrapper.eq(WcsInboundAbnormalMission::getMissionStatus,status);
        return wcsInboundAbnormalMissionMapper.selectList(wcsInboundAbnormalMissionLambdaQueryWrapper);
    }

    @Override
    public boolean deleteId(Integer id) {
        return wcsInboundAbnormalMissionMapper.deleteById(id) >0;
    }
}
